package com.zzy.chapter2.pojo;


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Iterator;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ProductItemComposite extends AbstractProductItem {

    private Integer id;

    private String name;

    private Integer pid;

    private List<AbstractProductItem> child;

    @Override
    public void addProductItem(AbstractProductItem productItem) {
        this.child.add(productItem);
    }

    @Override
    public void deleteProductItem(AbstractProductItem productItem) {
        List<AbstractProductItem> itemList = this.getChild();
        if (itemList == null || itemList.size() == 0) {
            return;
        }
        Iterator<AbstractProductItem> iterator = itemList.iterator();
        while (iterator.hasNext()) {
            ProductItemComposite productItemComposite = (ProductItemComposite) iterator.next();
            ProductItemComposite delItem = (ProductItemComposite) productItem;
            if (productItemComposite.getId().equals(delItem.getId())) {
                iterator.remove();
                break;
            }
        }
    }
}
